/**
 * 
 */
package info.niwota.frames;

import android.os.Handler;

/**
 * @author qiangli
 *
 */
public class ProgressMonitor {

	public CharSequence message;
	
	private boolean aborted;
	
	private Handler handler;
	
	public ProgressMonitor() {
	}

	private Handler timer = new Handler();

	private Runnable task;

	public void abort() {
		this.aborted = true;
	}
	
	public boolean isAborted() {
		return aborted;
	}
	
	public void send(CharSequence message) {
		this.message = message;
		if (handler != null) {
			handler.sendEmptyMessage(0);
		}
	}
	
	public void send(int what, CharSequence message) {
		this.message = message;
		if (handler != null) {
			handler.sendEmptyMessage(what);
		}
	}

	public void start(Handler handler) {
		this.aborted = false;
		this.handler = handler;
	}
	
	public void start(Handler handler, long delay, Runnable task) {
		this.aborted = false;
		this.handler = handler;
		
		this.task = task;
		
		timer.removeCallbacks(task);
		timer.postDelayed(task, delay);
	}

	public void clear() {
		
		aborted = true;
		if (task != null) {
			timer.removeCallbacks(task);
		}
	}
}
